Imports System.Web.Configuration
Imports System.Configuration

Public Class nFlickrViewerConfigurationSection
    Inherits ConfigurationSection
    Public Shadows Function IsReadOnly() As Boolean
        Return False
    End Function

    <ConfigurationProperty("FlickrConfigurationElement")> _
        Public ReadOnly Property FlickrConfigurationElement() As FlickrConfigurationElement
        Get
            Return TryCast(Me("FlickrConfigurationElement"), FlickrConfigurationElement)
        End Get
    End Property
    <ConfigurationProperty("AdminConfigurationElement")> _
    Public ReadOnly Property AdminConfigurationElement() As AdminConfigurationElement
        Get
            Return TryCast(Me("AdminConfigurationElement"), AdminConfigurationElement)
        End Get
    End Property

    <ConfigurationProperty("SimpleViewerConfigurationElement")> _
Public ReadOnly Property SimpleViewerConfigurationElement() As SimpleViewerConfigurationElement
        Get
            Return TryCast(Me("SimpleViewerConfigurationElement"), SimpleViewerConfigurationElement)
        End Get
    End Property


End Class


Public Class SimpleViewerConfigurationElement
    Inherits ConfigurationElement
    Public Shadows Function IsReadOnly() As Boolean
        Return False
    End Function
    <ConfigurationProperty("maxImageHeight", IsRequired:=False, DefaultValue:=800)> _
    Public Property maxImageHeight() As Integer
        Get
            Return CType(Me("maxImageHeight"), Integer)
        End Get
        Set(ByVal value As Integer)
            Me("maxImageHeight") = value
        End Set

    End Property
    <ConfigurationProperty("maxImageWidth", IsRequired:=False, DefaultValue:=800)> _
Public Property maxImageWidth() As Integer
        Get
            Return CType(Me("maxImageWidth"), Integer)
        End Get
        Set(ByVal value As Integer)
            Me("maxImageWidth") = value
        End Set
    End Property

    <ConfigurationProperty("textColor", IsRequired:=False, DefaultValue:="White")> _
Public Property textColor() As String
        Get
            Return TryCast(Me("textColor"), String)
        End Get
        Set(ByVal value As String)
            Me("textColor") = value
        End Set
    End Property

    <ConfigurationProperty("frameColor", IsRequired:=False, DefaultValue:="Black")> _
Public Property frameColor() As String
        Get
            Return TryCast(Me("frameColor"), String)
        End Get
        Set(ByVal value As String)
            Me("frameColor") = value
        End Set
    End Property

    <ConfigurationProperty("frameWidth", IsRequired:=False, DefaultValue:=10)> _
Public Property frameWidth() As Integer
        Get
            Return CType(Me("frameWidth"), Integer)
        End Get
        Set(ByVal value As Integer)
            Me("frameWidth") = value
        End Set
    End Property

    <ConfigurationProperty("stagePadding", IsRequired:=False, DefaultValue:=40)> _
Public Property stagePadding() As Integer
        Get
            Return CType(Me("stagePadding"), Integer)
        End Get
        Set(ByVal value As Integer)
            Me("stagePadding") = value
        End Set
    End Property

    <ConfigurationProperty("thumbnailColumns", IsRequired:=False, DefaultValue:=4)> _
Public Property thumbnailColumns() As Integer
        Get
            Return CType(Me("thumbnailColumns"), Integer)
        End Get
        Set(ByVal value As Integer)
            Me("thumbnailColumns") = value
        End Set
    End Property


    <ConfigurationProperty("thumbnailRows", IsRequired:=False, DefaultValue:=4)> _
Public Property thumbnailRows() As Integer
        Get
            Return CType(Me("thumbnailRows"), Integer)
        End Get
        Set(ByVal value As Integer)
            Me("thumbnailRows") = value
        End Set
    End Property


    <ConfigurationProperty("navDirection", IsRequired:=False, DefaultValue:="LTR")> _
Public Property navDirection() As String
        Get
            Return TryCast(Me("navDirection"), String)
        End Get
        Set(ByVal value As String)
            Me("navDirection") = value
        End Set
    End Property

    <ConfigurationProperty("navPosition", IsRequired:=False, DefaultValue:="right")> _
Public Property navPosition() As String
        Get
            Return TryCast(Me("navPosition"), String)
        End Get
        Set(ByVal value As String)
            Me("navPosition") = value
        End Set
    End Property

    <ConfigurationProperty("galleryTitle", IsRequired:=False, DefaultValue:="My Portfolio")> _
Public Property galleryTitle() As String
        Get
            Return TryCast(Me("galleryTitle"), String)
        End Get
        Set(ByVal value As String)
            Me("galleryTitle") = value
        End Set
    End Property
    <ConfigurationProperty("backgroundImagePath", IsRequired:=False, DefaultValue:="")> _
Public Property backgroundImagePath() As String
        Get
            Return TryCast(Me("backgroundImagePath"), String)
        End Get
        Set(ByVal value As String)
            Me("backgroundImagePath") = value
        End Set
    End Property
    <ConfigurationProperty("paypalCartLink", IsRequired:=False, DefaultValue:="")> _
Public Property paypalCartLink() As String
        Get
            Return TryCast(Me("paypalCartLink"), String)
        End Get
        Set(ByVal value As String)
            Me("paypalCartLink") = value
        End Set
    End Property
    <ConfigurationProperty("paypalEmailAddress", IsRequired:=False, DefaultValue:="")> _
Public Property paypalEmailAddress() As String
        Get
            Return TryCast(Me("paypalEmailAddress"), String)
        End Get
        Set(ByVal value As String)
            Me("paypalEmailAddress") = value
        End Set
    End Property
    <ConfigurationProperty("contactMeEmailAddress", IsRequired:=False, DefaultValue:="")> _
Public Property contactMeEmailAddress() As String
        Get
            Return TryCast(Me("contactMeEmailAddress"), String)
        End Get
        Set(ByVal value As String)
            Me("contactMeEmailAddress") = value
        End Set
    End Property
    <ConfigurationProperty("paypalPricePerItem", IsRequired:=False, DefaultValue:=25)> _
Public Property paypalPricePerItem() As Integer
        Get
            Return CType(Me("paypalPricePerItem"), Integer)
        End Get
        Set(ByVal value As Integer)
            Me("paypalPricePerItem") = value
        End Set
    End Property

    <ConfigurationProperty("enableRightClickOpen", IsRequired:=False, DefaultValue:=False)> _
Public Property enableRightClickOpen() As Boolean
        Get
            Return CType(Me("enableRightClickOpen"), Boolean)
        End Get
        Set(ByVal value As Boolean)
            Me("enableRightClickOpen") = value
        End Set
    End Property


    <ConfigurationProperty("includeImageLink", IsRequired:=False, DefaultValue:=False)> _
Public Property includeImageLink() As Boolean
        Get
            Return CType(Me("includeImageLink"), Boolean)
        End Get
        Set(ByVal value As Boolean)
            Me("includeImageLink") = value
        End Set
    End Property

    <ConfigurationProperty("includeImageName", IsRequired:=False, DefaultValue:=False)> _
Public Property includeImageName() As Boolean
        Get
            Return CType(Me("includeImageName"), Boolean)
        End Get
        Set(ByVal value As Boolean)
            Me("includeImageName") = value
        End Set
    End Property

    <ConfigurationProperty("includePaypalLink", IsRequired:=False, DefaultValue:=False)> _
Public Property includePaypalLink() As Boolean
        Get
            Return CType(Me("includePaypalLink"), Boolean)
        End Get
        Set(ByVal value As Boolean)
            Me("includePaypalLink") = value
        End Set
    End Property

End Class




Public Class FlickrConfigurationElement
    Inherits ConfigurationElement
    Public Shadows Function IsReadOnly() As Boolean
        Return False
    End Function





    <ConfigurationProperty("flickrApiKey", IsRequired:=True)> _
    Public Property flickrApiKey() As String
        Get
            Return TryCast(Me("flickrApiKey"), String)
        End Get
        Set(ByVal value As String)
            Me("flickrApiKey") = value
        End Set

    End Property

    <ConfigurationProperty("runningMode", IsRequired:=False, DefaultValue:="user")> _
     Public Property runningMode() As String
        Get
            Return TryCast(Me("runningMode"), String)
        End Get
        Set(ByVal value As String)
            Me("runningMode") = value
        End Set
    End Property
    <ConfigurationProperty("flickrSetId", IsRequired:=False)> _
   Public Property flickrSetId() As String
        Get
            Return TryCast(Me("flickrSetId"), String)
        End Get
        Set(ByVal value As String)
            Me("flickrSetId") = value
        End Set
    End Property
    <ConfigurationProperty("flickrTag", IsRequired:=False)> _
   Public Property flickrTag() As String
        Get
            Return TryCast(Me("flickrTag"), String)
        End Get
        Set(ByVal value As String)
            Me("flickrTag") = value
        End Set
    End Property
    <ConfigurationProperty("flickrUserId", IsRequired:=False)> _
   Public Property flickrUserId() As String
        Get
            Return TryCast(Me("flickrUserId"), String)
        End Get
        Set(ByVal value As String)
            Me("flickrUserId") = value
        End Set
    End Property
    <ConfigurationProperty("flickrSearchTerm", IsRequired:=False)> _
   Public Property flickrSearchTerm() As String
        Get
            Return TryCast(Me("flickrSearchTerm"), String)
        End Get
        Set(ByVal value As String)
            Me("flickrSearchTerm") = value
        End Set
    End Property
    <ConfigurationProperty("flickrImageSize", IsRequired:=False, DefaultValue:="med")> _
   Public Property flickrImageSize() As String
        Get
            Return TryCast(Me("flickrImageSize"), String)
        End Get
        Set(ByVal value As String)
            Me("flickrImageSize") = value
        End Set
    End Property
    <ConfigurationProperty("flickrStaticImageUrl", IsRequired:=False)> _
   Public Property flickrStaticImageUrl() As String
        Get
            Return TryCast(Me("flickrStaticImageUrl"), String)
        End Get
        Set(ByVal value As String)
            Me("flickrStaticImageUrl") = value
        End Set
    End Property
    <ConfigurationProperty("flickrSortOrder", IsRequired:=False)> _
Public Property flickrSortOrder() As String
        Get
            Return TryCast(Me("flickrSortOrder"), String)
        End Get
        Set(ByVal value As String)
            Me("flickrSortOrder") = value
        End Set
    End Property

    <ConfigurationProperty("flickrCacheInterval", IsRequired:=False, DefaultValue:=43200)> _
Public Property flickrCacheInterval() As Integer
        Get
            Return CType(Me("flickrCacheInterval"), Integer)
        End Get
        Set(ByVal value As Integer)
            Me("flickrCacheInterval") = value
        End Set
    End Property
    <ConfigurationProperty("flickrGroupId", IsRequired:=False)> _
   Public Property flickrGroupId() As String
        Get
            Return TryCast(Me("flickrGroupId"), String)
        End Get
        Set(ByVal value As String)
            Me("flickrGroupId") = value
        End Set
    End Property
    <ConfigurationProperty("flickrMaxNoResults", IsRequired:=False, DefaultValue:=50)> _
   Public Property flickrMaxNoResults() As Integer
        Get
            Return CType(Me("flickrMaxNoResults"), Integer)
        End Get
        Set(ByVal value As Integer)
            Me("flickrMaxNoResults") = value
        End Set
    End Property
End Class

Public Class AdminConfigurationElement
    Inherits ConfigurationElement
    Public Shadows Function IsReadOnly() As Boolean
        Return False
    End Function
    <ConfigurationProperty("password", IsRequired:=True)> _
    Public Property password() As String
        Get
            Return TryCast(Me("password"), String)
        End Get
        Set(ByVal value As String)
            Me("password") = value
        End Set
    End Property
End Class

Public Class CacheConfig
    Private Shared config As nFlickrViewerConfigurationSection = ConfigurationManager.GetSection("nFlickrViewer")
    Shared myCacheConfig As CacheConfig 'Private
    Private doUseCache As Boolean = False 'whether or not to use cache
    Private doCreateCache As Boolean = False 'if docache, and time expired

    Public Property DoUseCacheProp() As Boolean
        Get
            Return doUseCache
        End Get
        Set(ByVal value As Boolean)
            doUseCache = value
        End Set
    End Property
    Public Property DoCreateCacheProp() As Boolean
        Get
            Return doCreateCache
        End Get
        Set(ByVal value As Boolean)
            doCreateCache = value
        End Set
    End Property
    Public Shared Function GetInstance() As CacheConfig
        'If myCacheConfig Is Nothing Then
        myCacheConfig = New CacheConfig()
        ' End If
        Return myCacheConfig
    End Function
    Sub New()

        Dim cacheInterval As Integer = config.FlickrConfigurationElement.flickrCacheInterval  'timespan in seconds from config

        If cacheInterval > 0 Then
            doUseCache = True

            'check if xml exists
            If Not System.IO.File.Exists(HttpContext.Current.Server.MapPath("flickrData.xml")) Then
                doCreateCache = True
            Else
                'get timestamp from xml
                Dim fi As System.IO.FileInfo = New System.IO.FileInfo(HttpContext.Current.Server.MapPath("flickrData.xml"))
                If (DateTime.Now - fi.LastWriteTime).Seconds > cacheInterval Then 'the cache is expired
                    doCreateCache = True
                End If
            End If
        Else
            doCreateCache = True ' if cache is disabled, always use aspx
        End If
    End Sub

End Class
